﻿// OperatorTreeCtrl.cpp: 实现文件
//

#include "stdafx.h"
#include "zChecker.h"
#include "OperatorCatalogTreeCtrl.h"


// COperatorTreeCtrl

IMPLEMENT_DYNAMIC(COperatorCatalogTreeCtrl, CTreeCtrl)

COperatorCatalogTreeCtrl::COperatorCatalogTreeCtrl()
{

}

COperatorCatalogTreeCtrl::~COperatorCatalogTreeCtrl()
{
}


BEGIN_MESSAGE_MAP(COperatorCatalogTreeCtrl, CTreeCtrl)
	ON_WM_CREATE()
END_MESSAGE_MAP()



// COperatorTreeCtrl 消息处理程序




int COperatorCatalogTreeCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CTreeCtrl::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO:  在此添加您专用的创建代码
	AttachBitmap();

	return 0;
}


void COperatorCatalogTreeCtrl::AttachBitmap()
{
	CBitmap bmp;
	if (bmp.LoadBitmap(IDB_BITMAP_CHECK_MODEL_TREE_16))
	{
		m_il.Create(16, 16, ILC_COLOR32 | ILC_MASK, 0, 0);
		m_il.Add(&bmp, RGB(255, 0, 255));
		int nCount = m_il.GetImageCount();
		for (int i = 1; i < nCount; i++)         //support 16
		{
			m_il.SetOverlayImage(i, i);
		}
		SetImageList(&m_il, TVSIL_NORMAL);
	}
}

void COperatorCatalogTreeCtrl::Populate()
{
	this->DeleteAllItems();
	this->InsertItem("检查算子库", OPER_TREE_ROOT_NODE, OPER_TREE_ROOT_NODE, TVI_ROOT);
	HTREEITEM hRoot = this->GetRootItem();

	ck::ElementRepository* pRepository = ck::ElementRepository::GetInstance();
	ck::CheckElement* pElement = NULL;
	ck::EnumCheckElement* pElements = NULL;
	pElements = pRepository->GetElements();
	if (pElements)
	{
		pElements->Reset();
		while ((pElement = pElements->Next()))
		{
			const char* szName = pElement->GetName();
			z_uint32 code = pElement->GetCode();

			HTREEITEM hItem = this->InsertItem(szName, OPER_TREE_ELEM_NODE, OPER_TREE_ELEM_NODE, hRoot);
			this->SetItemData(hItem, (DWORD)code);

			PopulateSubElement(code, hItem);
		}
		pElements->Release();

		this->Expand(hRoot, TVE_EXPAND);
	}
}

void COperatorCatalogTreeCtrl::PopulateSubElement(z_uint32 nParentCode, HTREEITEM hParentItem)
{
	ck::ElementRepository* pRepository = ck::ElementRepository::GetInstance();
	ck::CheckElement* pElement = NULL;
	ck::EnumCheckElement* pElements = NULL;
	pElements = pRepository->GetChildElements(nParentCode);
	if (pElements)
	{
		pElements->Reset();
		while ((pElement = pElements->Next()))
		{
			const char* szName = pElement->GetName();
			z_uint32 code = pElement->GetCode();
			HTREEITEM hItem = this->InsertItem(szName, OPER_TREE_SUBE_NODE, OPER_TREE_SUBE_NODE, hParentItem);
			this->SetItemData(hItem, (DWORD)code);

			PopulateItem(code, hItem);
		}
		pElements->Release();

		this->Expand(hParentItem, TVE_EXPAND);
	}
}

void COperatorCatalogTreeCtrl::PopulateItem(z_uint32 nParentCode, HTREEITEM hParentItem)
{

	ck::CheckModelManager* pModelManager = ck::CheckModelManager::GetInstance();

	ck::ElementRepository* pRepository = ck::ElementRepository::GetInstance();
	ck::CheckElement* pElement = NULL;
	ck::EnumCheckElement* pElements = NULL;
	pElements = pRepository->GetChildElements(nParentCode);

	if (pElements)
	{
		pElements->Reset();
		while ((pElement = pElements->Next()))
		{
			const char* szName = pElement->GetName();
			z_uint32 nItemCode = pElement->GetCode();

			HTREEITEM hItem = this->InsertItem(szName, OPER_TREE_ITEM_NODE, OPER_TREE_ITEM_NODE, hParentItem);
			this->SetItemData(hItem, (DWORD)nItemCode);

		}
		pElements->Release();

		this->Expand(hParentItem, TVE_EXPAND);
	}
}


//#define		OPER_TREE_ROOT_NODE	0
//#define		OPER_TREE_OPER_NODE	1
//#define		OPER_TREE_SUBE_NODE	2
//#define		OPER_TREE_ITEM_NODE	3
int	COperatorCatalogTreeCtrl::GetNodeType(HTREEITEM hItem)
{
	int nType = -1;
	HTREEITEM hRoot = GetRootItem();

	if (hRoot == hItem)
	{
		return OPER_TREE_ROOT_NODE;
	}
	else
	{
		HTREEITEM hParent = GetParentItem(hItem);
		if (hParent == hRoot)
		{
			return OPER_TREE_ELEM_NODE;
		}
		else
		{
			hItem = hParent;
			hParent = GetParentItem(hItem);
			if (hParent == hRoot)
			{
				return OPER_TREE_SUBE_NODE;
			}
			else
			{
				hItem = hParent;
				hParent = GetParentItem(hItem);
				if (hParent == hRoot)
				{
					return OPER_TREE_ITEM_NODE;
				}
				else
				{
					hItem = hParent;
					hParent = GetParentItem(hItem);
					if (hParent == hRoot)
					{
						return OPER_TREE_OPER_NODE;
					}
				}
			}

		}
	}

	return nType;
}